﻿using Ordering.Domain.Exceptions;
using Ordering.Domain.Seedwork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordering.Domain.AggregatesModel.OrderAggregate
{
    /// <summary>
    /// 订单状态
    /// </summary>
    public class OrderStatus
        : Enumeration
    {
        /// <summary>
        /// 下单
        /// </summary>
        public static OrderStatus Submitted = new OrderStatus(1, nameof(Submitted).ToLowerInvariant());
        /// <summary>
        /// 支付
        /// </summary>
        public static OrderStatus Paid = new OrderStatus(2, nameof(Paid).ToLowerInvariant());
        /// <summary>
        /// 接收订单
        /// </summary>
        public static OrderStatus ReceiveConfirmed = new OrderStatus(3, nameof(ReceiveConfirmed).ToLowerInvariant());
        /// <summary>
        /// 配送
        /// </summary>
        public static OrderStatus Delivery = new OrderStatus(4, nameof(Delivery).ToLowerInvariant());
        /// <summary>
        /// 完成
        /// </summary>
        public static OrderStatus Completed = new OrderStatus(5, nameof(Completed).ToLowerInvariant());
        /// <summary>
        /// 取消
        /// </summary>
        public static OrderStatus Cancelled = new OrderStatus(6, nameof(Cancelled).ToLowerInvariant());

        public OrderStatus(int id, string name)
            : base(id, name)
        {
        }

        public static IEnumerable<OrderStatus> List() =>
            new[] { Submitted, Paid, ReceiveConfirmed, Delivery, Completed, Cancelled };

        public static OrderStatus FromName(string name)
        {
            var state = List()
                .SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));

            if (state == null)
            {
                throw new OrderingDomainException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}");
            }

            return state;
        }

        public static OrderStatus From(int id)
        {
            var state = List().SingleOrDefault(s => s.Id == id);

            if (state == null)
            {
                throw new OrderingDomainException($"Possible values for OrderStatus: {String.Join(",", List().Select(s => s.Name))}");
            }

            return state;
        }
    }
}
